package com.crm.util;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.crm.model.BaseModel;
import com.crm.model.SysUser;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.io.Files;
import com.google.common.primitives.Bytes;

/**
 * 
 * @author zhaoyulong
 *
 */
public class CSVUtil {
	private final static String DEFAULT_DELIMITER = ",";
	private final static String DEFAULT_END = "\r\n";
	private final static byte commonCsvHead[] = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };

	public static void main(String[] args) throws UnsupportedEncodingException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
		SysUser s = new SysUser();
		s.setUserId("111");
		s.setAccount("xxx");
		SysUser s2 = new SysUser();
		s2.setUserId("222");
		s2.setAccount("yyy");
		List<SysUser> list = new ArrayList<SysUser>();
		list.add(s2);
		list.add(s);
		CSVUtil.export(reflectValueList(new SysUser(),list));
	}
	
	/**
	 * 
	 * @Description : 数据转换
	 * @author zealot
	 * @param @param source
	 * @param @return
	 * @param @throws UnsupportedEncodingException
	 * @param @throws IOException
	 * @return byte[]
	 * @version 创建时间 : 2016年11月16日 上午11:33:30
	 * @thorws
	 */
	public static byte[] export(List<List<String>> source) throws UnsupportedEncodingException, IOException {
		Preconditions.checkNotNull(source);
		StringBuilder sbBuilder = new StringBuilder();
		for (List<String> list : source) {
			sbBuilder.append(Joiner.on(DEFAULT_DELIMITER).join(list)).append(DEFAULT_END);
		}
		byte[] buff = Bytes.concat(commonCsvHead, sbBuilder.toString().getBytes(Charsets.UTF_8.toString()));
//		Files.write(Bytes.concat(commonCsvHead, sbBuilder.toString().getBytes(Charsets.UTF_8.toString())),
//				new File("/Users/zhaoyulong/Documents/workspace:11.csv"));
		return buff;
	}

	/**
	 * 
	 * @Description : csv数据组装
	 * @author zealot
	 * @param @param model
	 * @param @param list
	 * @param @return
	 * @param @throws SecurityException
	 * @param @throws NoSuchMethodException
	 * @param @throws IllegalArgumentException
	 * @param @throws IllegalAccessException
	 * @param @throws InvocationTargetException
	 * @param @throws InstantiationException
	 * @return List<List<String>>
	 * @version 创建时间 : 2016年11月16日 上午9:56:08
	 * @thorws
	 */
	public static List<List<String>> reflectValueList(Object model, List<? extends BaseModel> list)
			throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, InstantiationException {
		Field[] field = model.getClass().getDeclaredFields();
		String[] modelType = new String[field.length];
		List<List<String>> mainList = new ArrayList<>();
		List<String> str = null;
		for (int k = 0; k < list.size(); k++) {
			model = list.get(k);
			str = new ArrayList<>();
			for (int i = 0; i < field.length; i++) {
				String name = field[i].getName();
				String type = field[i].getGenericType().toString();
				modelType[i] = type;
				field[i].setAccessible(true);
				name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
				Method main = model.getClass().getMethod("get" + name);
				String v = main.invoke(model) == null ? "" : main.invoke(model).toString();
				str.add(v);
			}
			mainList.add(str);
		}
		return mainList;
	}
}
